home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / NTUMIN10.ARJ / CAMPD.C < prev    next >
C/C++ Source or Header  |  1992-03-09  |  13KB  |  385 lines

  1. /****************************************************************************
  2.  *
  3.  *      Program name : CAMPD.C
  4.  *
  5.  *      This is the actual minimization algorithm. Variation from the actual
  6.  *    CAMP algorithm is as follows:
  7.  *        1.    Minterms with adjacency 0 or 1 is minimised first
  8.  *        2.    Select next minterm with lowest adjacency wrt b-array
  9.  *        3.    To shrink the CPT, select an adjacent term, mi that has the
  10.  *            largest wi AND is already covered
  11.  *        4.  If more than one such adjacent terms are covered, select one
  12.  *        with the lowest adjacency wrt to b-array
  13.  *
  14.  * --------------------------------------------------------------------------
  15.  *    Copyright (c) 1992. All Rights Reserved. Nanyang Technological
  16.  *    University.
  17.  *
  18.  *    You are free to use, copy and distribute this software and its
  19.  *    documentation providing that:
  20.  *
  21.  *        NO FEE IS CHARGED FOR USE, COPYING OR DISTRIBUTION.
  22.  *
  23.  *        IT IS NOT MODIFIED IN ANY WAY.
  24.  *
  25.  *        THE COPYRIGHT NOTICE APPEAR IN ALL COPIES.
  26.  *
  27.  *    This program is provided "AS IS" without any warranty, expressed or
  28.  *    implied, including but not limited to fitness for any particular
  29.  *    purpose.
  30.  *
  31.  *    If you find NTUMIN fast, easy, and useful, a note or comment would be
  32.  *    appreciated. Please send to:
  33.  *
  34.  *        Boon-Tiong Tan or Othman Bin Ahmad
  35.  *        School of EEE
  36.  *        Nanyang Technological University
  37.  *        Nanyang Avenue
  38.  *        Singapore 2263
  39.  *        Republic of Singapore
  40.  *
  41.  ***************************************************************************/
  42.  
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #define mask8 255
  47.  
  48. unsigned char   *campd(a, b)              /* pointer to a & b array passed */
  49. unsigned char   *a, *b;
  50.  
  51. {
  52.    unsigned short   pterm, ma, mb, *pm, *pm1, pos;
  53.    unsigned  long   m, k, count, count1, topow();
  54.    register  long   i, wo, wi;
  55.    register  char   j;
  56.          char   test;
  57.    unsigned  char   *c, *d, *e, *s, *temp, adj0, adji;
  58.    unsigned  char   n, adj, adjacency(), nspm, cover;
  59.    unsigned  char   *adjacent(), *reduce(), *cpt(), *ssm(), adj_of_mi();
  60.  
  61.  
  62.    mb = *(b+2)<<8 | *(b+1);            /* no. of minterms in b-array */
  63.  
  64.    n    = *a;                          /* no. of variables */
  65.    nspm = *(a+3);                      /* no. of bytes/minterm */
  66.    ma = *(a+2)<<8 | *(a+1);            /* no. of minterms in a-array */
  67.  
  68.    temp = (unsigned char *) malloc(nspm+1);        /* temporary storage */
  69.    if (temp == 0)
  70.       {
  71.      printf("Out of memory -- CAMPD, *temp\n");
  72.      printf("Program terminated - 1\n");
  73.      exit(0);
  74.       }
  75.  
  76.    s = (unsigned char *) malloc(4);                /* header of s-array */
  77.    if (s == 0)
  78.       {
  79.      printf("Out of memory -- CAMPD, *s\n");
  80.      printf("Program terminated - 2\n");
  81.      exit(0);
  82.       }
  83.  
  84.    pterm = 0;                                /* no. of product term */
  85.  
  86.    /***
  87.     ***  MINIMIZE ALL MINTERMS WITH ADJACENCY OF 0 & 1 FIRST
  88.     ***/
  89.  
  90.    for (i=0; i<mb; i++)                      /* for adj = 0 or 1 */
  91.       {
  92.      *b = n;                             /* assign back to n */
  93.  
  94.      memcpy(temp, (b+4+nspm*i), nspm);   /* pick a minterm from b-array */
  95.      c = adjacent(temp, a, 1);           /* obtain the adjacent terms */
  96.      adj = *(c+1);                       /* adjacency of minterm */
  97.  
  98.      if (adj <= 1)                       /* adjacency 0 or 1 */
  99.         {
  100.            d = cpt(c);                   /* generate CPT */
  101.  
  102.            s = (unsigned char *) realloc(s,4+n*(pterm+1));   /* more space */
  103.            if (s == 0)
  104.           {
  105.              printf("Out of memory -- CAMPD, *s\n");
  106.              printf("Program terminated - 3\n");
  107.              exit(0);
  108.           }
  109.            memcpy ((s+4+n*pterm),d,n);   /* add CPT to soln array */
  110.            pterm++;                      /* product term count */
  111.  
  112.            b = reduce(c,b,i);            /* remove minterms covered from b-array */
  113.            i = (char) *b;                /* index pointer of b-array */
  114.            mb = *(b+2)<<8 | *(b+1);      /* value of mb altered in b-array */
  115.  
  116.            free(d);                    /* free pointer */
  117.         }
  118.      free(c);                            /* free pointer */
  119.       }
  120.  
  121.     /***
  122.      ***  MINIMIZE MINTERMS WITH ADJACENCY GREATER THAN 1
  123.      ***/
  124.  
  125.     while (mb > 0)
  126.        {
  127.       adj0 = 255;                     /* max for 8-bit */
  128.  
  129.       *b = n;                         /* assign back to n */
  130.  
  131.       /***
  132.        ***  SELECT 1ST MINTERM WITH LOWEST ADJACENCY WRT B-ARRAY
  133.        ***/
  134.  
  135.       for (i=0; i<mb; i++)
  136.          {
  137.         memcpy(temp, (b+4+nspm*i), nspm);    /* pick a minterm */
  138.         adji = adjacency(temp, b);           /* obtain adjacency */
  139.  
  140.         if (adji<adj0)                       /* look for lowest adj */
  141.            {
  142.               adj0 = adji;
  143.               pos = i;                       /* position of minterm */
  144.            }
  145.           }
  146.        memcpy(temp, (b+4+nspm*pos), nspm);       /* pick the required minterm */
  147.  
  148.        /***
  149.         ***  GENERATE SSM AND TEST FOR COVERAGE BY FUNCTION
  150.         ***/
  151.  
  152.        c = adjacent(temp, a, 255);          /* obtain adjacent terms */
  153.        adj = *(c+1);                        /* adjacency of minterms */
  154.        d = cpt(c);                          /* generate CPT */
  155.        e = ssm(d);                          /* generate SSM */
  156.        m = topow(*(e+1));                   /* no. of SSM terms */
  157.  
  158.        for (j=0; j<m; j++)                  /* check for SSM coverage */
  159.           {
  160.          memcpy (temp, (e+4+nspm*j), nspm);  /* pick SSM term */
  161.          test = exist (temp, a, ma);
  162.          if (test == -1)                /* SSM term not in a-array */
  163.              break;
  164.           }
  165.  
  166.        /***
  167.         ***  ALL SSM COVERED BY THE FUNCTION, SELECT CPT AS PRODUCT TERM
  168.         ***/
  169.  
  170.        if (test == 0)                       /* all SSM's covered by fn */
  171.           {
  172.          if (m > 65536)                 /* too many SSM terms */
  173.             {
  174.                printf("Product Term Too Huge \n");
  175.                printf("Program terminated \n");
  176.                exit(0);
  177.             }
  178.          *(e+1) = (m-1) & mask8;        /* no. of SSM terms minus 1 */
  179.          *(e+2) = (m-1)>>8 & mask8;
  180.          b = reduce(e,b,i);             /* remove minterms covered from b-array */
  181.          mb = *(b+2)<<8 | *(b+1);       /* no. of minterms in b-array */
  182.  
  183.          s = (unsigned char *) realloc(s, 4+n*(pterm+1)); /* more space */
  184.          if (s == 0)
  185.             {
  186.                printf("Out of memory -- CAMPD, *s\n");
  187.                printf("Program terminated - 4\n");
  188.                exit(0);
  189.             }
  190.          memcpy((s+4+n*pterm),d,n);     /* add CPT to soln array */
  191.          pterm++;                       /* no. of product terms */
  192.  
  193.          free(d);                       /* free pointers */
  194.          free(e);
  195.           }
  196.        else
  197.           {
  198.          /***
  199.           ***  SSM NOT COVERED BY THE FUNCTION
  200.           ***/
  201.  
  202.          free(d);                       /* free pointer */
  203.          cover =0;                      /* coverage status */
  204.  
  205.          while (!cover)        /* do until shrinked SSM is covered */
  206.             {
  207.                /***
  208.             ***  OBTAIN AN ARRAY, PM OF POSITIONS OF ADJACENT TERMS, MI WITH THE LARGEST WI
  209.             ***/
  210.  
  211.                wo = -1;                        /* initial value */
  212.                for (j=0; j<adj; j++)           /* do for all adjacent terms */
  213.               {
  214.                  memcpy(temp,(c+4+nspm*(j+1)),nspm); /* pick adjacent term */
  215.  
  216.                  wi = adj_of_mi(temp,e,a);           /* obtain wi */
  217.                  if (wi>wo)
  218.                 {
  219.                    if (wo != -1)                 /* not the first */
  220.                       free(pm);
  221.                    wo = wi;                      /* new lowest value */
  222.                    count = 1;                    /* reset count to 1 */
  223.  
  224.                    pm = (unsigned short *) malloc(2);  /* space for pm */
  225.                    if (pm==0)
  226.                       {
  227.                      printf("Out of memory -- CAMPD, *pm\n");
  228.                      printf("Program terminated - 5\n");
  229.                      exit(0);
  230.                       }
  231.                    *pm = j;                      /* first element */
  232.                 }
  233.                  else if (wi==wo)                    /* wi as large */
  234.                 {
  235.                    count++;                      /* no. of element in pm-array */
  236.  
  237.                    pm = (unsigned short *) realloc (pm, 2*count);  /* more space */
  238.                    if (pm==0)
  239.                       {
  240.                      printf("Out of memory -- CAMPD, *pm\n");
  241.                      printf("Program terminated - 6\n");
  242.                      exit(0);
  243.                       }
  244.                    *(pm+count-1) = j;            /* elements of pm-array */
  245.                 }
  246.               }
  247.                free(e);                                  /* free pointer */
  248.  
  249.                /***
  250.             ***  DETERMINE FROM PM-ARRAY, AN ARRAY, PM1 OF POSITION OF ADJACENT TERMS
  251.             ***  THAT HAVE ALREADY BEEN COVERED
  252.             ***/
  253.  
  254.                count1 = count;                           /* no. of elements in pm & pm1 */
  255.  
  256.                pm1 = (unsigned short *) malloc(2);       /* space for pm1 */
  257.                if (pm1==0)
  258.               {
  259.                  printf("Out of memory -- CAMPD, *pm1\n");
  260.                  printf("Program terminated - 7\n");
  261.                  exit(0);
  262.               }
  263.  
  264.                k = 0;
  265.                for (j=0; j<count; j++)              /* do for all element in pm-array */
  266.               {
  267.                  memcpy(temp, (c+4+nspm*(1+ *(pm+j))), nspm);    /* pick adj term */
  268.                  test = exist(temp,b,mb);
  269.                  if (test == -1)                               /* already covered */
  270.                 {
  271.                    memcpy((pm1+k++), (pm+j), 2);       /* transfer pos to pm1 */
  272.                    count1--;                                       /* reduced */
  273.  
  274.                    pm1 = (unsigned short *) realloc(pm1, 2*(k+1)); /*  more space */
  275.                    if (pm1==0)
  276.                       {
  277.                      printf("Out of memory -- CAMPD, *pm1\n");
  278.                      printf("Program terminated - 8\n");
  279.                      exit(0);
  280.                       }
  281.                 }
  282.               }
  283.  
  284.                /***
  285.             ***  SELECT FROM PM1-ARRAY, THE 1ST ADJACENT TERM, MI WITH THE LOWEST
  286.             ***  ADJACENCY WRT B-ARRAY
  287.             ***/
  288.  
  289.                adj0 = 255;                     /* max of 8-bit */
  290.                for (j=0; j<k; j++)             /* do for all in pm1-array */
  291.               {
  292.                  memcpy(temp,(c+4+nspm*(1+*(pm1+j))),nspm);  /* pick an adj term */
  293.                  adji = adjacency(temp,b);                   /* obtain adjacency */
  294.  
  295.                  if (adji<adj0)                        /* new lowest found */
  296.                 {
  297.                    adj0 = adji;                    /* update lowest */
  298.                    pos = *(pm1+j);                 /* position of lowest adj term */
  299.                 }
  300.               }
  301.  
  302.                if (count1==count)                /* select the 1st if all not covered */
  303.               pos = *pm;
  304.  
  305.                adj--;                            /* no. of adj terms in c-array */
  306.                *(c+1) = adj;                     /* adjacency after shrinking CPT */
  307.  
  308.                free(pm);                         /* free pointers */
  309.                free(pm1);
  310.  
  311.                /***
  312.             ***  SHRINK CPT (REMOVE 1 ADJACENT TERM FROM C-ARRAY), GENERATE NEW CPT, SSM
  313.             ***  AND TEST FOR COVERAGE BY FUNCTION
  314.             ***/
  315.  
  316.                memcpy((c+4+nspm*(1+pos)), (c+4+nspm*(2+pos)), nspm*(adj-pos));  /* shrink CPT */
  317.  
  318.                c = (unsigned char*) realloc(c, 4+nspm*(1+adj));   /* reduce space */
  319.                if (c==0)
  320.               {
  321.                  printf("Out of memory -- CAMPD, *c\n");
  322.                  printf("Program terminated - 9\n");
  323.                  exit(0);
  324.               }
  325.  
  326.                d = cpt(c);                  /* generate CPT */
  327.                e = ssm(d);                  /* generate SSM */
  328.                m = topow(*(e+1));           /* no. of SSM terms */
  329.  
  330.                for (k=0; k<m; k++)          /* check SSM coverage by function */
  331.               {
  332.                  memcpy(temp, (e+4+nspm*k), nspm);  /* pick SSM term */
  333.                  test = exist(temp,a,ma);
  334.                  if (test == -1)                    /* SSM term not covered */
  335.                 break;
  336.               }
  337.  
  338.                /***
  339.             ***  SHRINKED SSM COVERED BY FUNCTION, REDUCE B-ARRAY
  340.             ***  AND SELECT SHRINKED CPT AS PRODUCT TERM
  341.             ***/
  342.  
  343.                if (test==0)                   /* SSM covered */
  344.               {
  345.                  if (m > 65536)                  /* too many SSM terms */
  346.                 {
  347.                    printf("Product Term Too Huge \n");
  348.                    printf("Program terminated \n");
  349.                    exit(0);
  350.                 }
  351.                  *(e+1) = (m-1) & mask8;         /* no. of SSM terms minus 1 */
  352.                  *(e+2) = (m-1)>>8 & mask8;
  353.                  b = reduce(e, b, i);     /* remove minterms covered from b-array */
  354.                  mb = *(b+2)<<8 | *(b+1); /* no. of minterms in b-array */
  355.  
  356.                  s = (unsigned char *) realloc(s, 4+n*(pterm+1));  /* more space */
  357.                  if (s==0)
  358.                 {
  359.                    printf("Out of memory -- CAMPD, *s\n");
  360.                    printf("Program terminated - 10\n");
  361.                    exit(0);
  362.                 }
  363.                  memcpy ((s+4+n*pterm), d, n);      /* add shrinked CPT to soln array */
  364.                  pterm++;                           /* no. of product terms */
  365.  
  366.                  cover = 1;                         /* coverage status */
  367.                  free(e);                           /* free pointer */
  368.               }
  369.                free (d);                      /* free pointer */
  370.             }
  371.           }
  372.       free(c);                   /* free pointer */
  373.       }
  374.    *s = n;                           /* no. of variables */
  375.    *(s+1) = pterm & mask8;           /* no. of product terms in 2 bytes */
  376.    *(s+2) = pterm>>8 & mask8;
  377.  
  378.    free(temp);                       /* free pointer */
  379.    free(a);
  380.    free(b);
  381.  
  382.    return(s);                        /* return solution array */
  383. }
  384.  
  385.